﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;

public partial class Admin_UserMaintain : System.Web.UI.Page
{
	public ColumnComponent clsColumnComp;
	public int nColumnID;
	public UserGroupComponent clsUserGroupComp;
	public UserComponent clsUserComp;
	public DepartmentComponent clsDepartmentComp = new DepartmentComponent();

	protected void Page_Load(object sender, EventArgs e)
	{
		clsColumnComp = new ColumnComponent();
		nColumnID = Convert.ToInt32(Request.QueryString["id"].Trim());
		clsUserGroupComp = new UserGroupComponent();
		clsUserComp = new UserComponent();

		if (!IsPostBack)
		{
			BindDropDownList(DdlSearchUserGroup);
			BindDropDownList(DdlUserGroupForAdd);
			BindDropDownListDepartment(DdlDepartmentForAdd);
			BindGridView(GrvUsers, 0);
			ViewState["UserGroupID"] = "0";
			Views.SetActiveView(ViewShow);


			BtnShow.Text = clsColumnComp.GetNameByID(nColumnID);

			
		}
	}

	#region 绑定控件
	/// <summary>
	/// <para>功能描述：绑定用户组下来列表</para>
	/// <para>输入参数：DropDownList ddl		要绑定的下来列表ID</para>
	/// <para>返回值：</para>
	/// <para>方法范围：protected</para>
	/// <para>作者：Tony Mucor</para>
	/// <para>日期：2013-08-02</para>
	/// <para>备注：</para>
	/// </summary>
	protected void BindDropDownList(DropDownList ddl)
	{
		ddl.DataSource = clsUserGroupComp.QueryAll();
		ddl.DataTextField = "UserGroupName";
		ddl.DataValueField = "UserGroupID";
		ddl.DataBind();
	}

	/// <summary>
	/// 绑定单位下来列表
	/// </summary>
	/// <param name="ddl"></param>
	protected void BindDropDownListDepartment(DropDownList ddl)
	{
		ddl.DataSource = clsDepartmentComp.QueryAll();
		ddl.DataTextField = "Name";
		ddl.DataValueField = "ID";
		ddl.DataBind();
	}

	/// <summary>
	/// <para>功能描述：	根据用户组ID查询用户，然后绑定用户GridView控件，如果用户组ID是0，则要查询所有用户</para>
	/// <para>输入参数：	GridView dv		要绑定的控件ID
	///			int nUserGroupID	用户组ID</para>
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-02</para>
	/// <para>备注：</para>
	/// </summary>
	protected void BindGridView(GridView dv, int nUserGroupID)
	{
		if (nUserGroupID != 0)
		{
			dv.DataSource = clsUserComp.QueryByUserGroupID(nUserGroupID);
		}
		else
		{
			dv.DataSource = clsUserComp.QueryAll();
		}
		dv.DataKeyNames = new string[] { "ID" };
		dv.DataBind();
	}

	/// <summary>
	/// <para>功能描述：	根据用户组ID查询用户，然后绑定用户GridView控件，如果用户组ID是0，则要查询所有用户</para>
	/// <para>输入参数：	GridView dv		要绑定的控件ID
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-02</para>
	/// <para>备注：</para>
	/// </summary>
	protected void BindGridView(GridView dv)
	{
		int nUserGroupID;

		nUserGroupID = Convert.ToInt32(ViewState["UserGroupID"]);
		if (nUserGroupID != 0)
		{
			dv.DataSource = clsUserComp.QueryByUserGroupID(nUserGroupID);
		}
		else
		{
			dv.DataSource = clsUserComp.QueryAll();
		}
		dv.DataKeyNames = new string[] { "ID" };
		dv.DataBind();
	}
	#endregion

	#region GridView控件事件相应函数
	protected void GrvUsers_OnRowDataBound(object sender, GridViewRowEventArgs e)
	{
		User clsUser;

		if (e.Row.RowIndex >= 0)
		{
			//e.Row.Attributes["onmouseover"] = "TmpColor=this.style.backgroundColor;this.style.backgroundColor='#f2f2f2';this.style.cursor='hand'";
			//e.Row.Attributes["onmouseout"] = "this.style.backgroundColor=TmpColor";
		}

		DropDownList ddl;

		ddl = (DropDownList)e.Row.FindControl("DdlUserGroupModify");
		if (ddl != null)
		{
			BindDropDownList(ddl);
			clsUser = clsUserComp.GetOneByID(Convert.ToInt32(((GridView) sender).DataKeys[e.Row.RowIndex].Value));
			ddl.SelectedValue = clsUser.UserGroupID.ToString();
		}

		DropDownList ddlDepartment = new DropDownList();
		ddlDepartment = (DropDownList)e.Row.FindControl("DdlDepartment");
		if (ddl != null)
		{
			BindDropDownListDepartment(ddlDepartment);
			clsUser = clsUserComp.GetOneByID(Convert.ToInt32(((GridView)sender).DataKeys[e.Row.RowIndex].Value));
			ddlDepartment.SelectedValue = clsUser.DepartmentID.ToString();
		}
	}

	protected void GrvUsers_OnRowEditing(object sender, GridViewEditEventArgs e)
	{
		((GridView)sender).EditIndex = e.NewEditIndex;
		BindGridView(((GridView)sender), Convert.ToInt32(ViewState["UserGroupID"]));
	}

	protected void GrvUsers_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
	{
		((GridView)sender).EditIndex = -1;
		BindGridView(((GridView)sender), Convert.ToInt32(ViewState["UserGroupID"]));
	}

	protected void GrvUsers_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
	{
		int nID;
		string strUsername;
		string strPassword;
		int nUserGroupID;
		int nDepartmentID;
		GridView dv;
		User clsUser;

		clsUser = new User();
		dv = (GridView)sender;

		nID = Convert.ToInt32(dv.DataKeys[e.RowIndex].Value);
		strUsername = ((TextBox)dv.Rows[e.RowIndex].FindControl("TxtUsername")).Text;
		strPassword = ((TextBox)dv.Rows[e.RowIndex].FindControl("TxtPassword")).Text;
		nUserGroupID = Convert.ToInt32(((DropDownList)dv.Rows[e.RowIndex].FindControl("DdlUserGroupModify")).SelectedValue);
		nDepartmentID = Convert.ToInt32(((DropDownList)dv.Rows[e.RowIndex].FindControl("DdlDepartment")).SelectedValue);

		clsUser.ID = nID;
		clsUser.Username = strUsername;
		clsUser.Password = strPassword;
		clsUser.UserGroupID = nUserGroupID;
		clsUser.DepartmentID = nDepartmentID;

		clsUserComp.Modify(clsUser);
		dv.EditIndex = -1;		// 取消编辑状态
		BindGridView(((GridView)sender), Convert.ToInt32(ViewState["UserGroupID"]));
	}

	protected void GrvUsers_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
	{
		int nID;
		User clsUser;
		GridView dv;

		dv = (GridView)sender;
		clsUser = new User();
		nID = Convert.ToInt32(dv.DataKeys[e.RowIndex].Value);
		clsUser.ID = nID;
		clsUserComp.Delete(clsUser);
		dv.EditIndex = -1;		// 取消编辑状态
		BindGridView(((GridView)sender), Convert.ToInt32(ViewState["UserGroupID"]));
	}
	
	protected void GrvUsers_OnRowDataRound(object sender, GridViewRowEventArgs e)
	{

	}

	protected void GrvUsers_OnDataBound(object sender, EventArgs e)
	{
		GridView dv;

		dv = (GridView)sender;
		LabNowPage.Text = (dv.PageIndex + 1).ToString();
		LabTotalPage.Text = dv.PageCount.ToString();

		if (dv.PageIndex == 0)
		{
			BtnFirst.Enabled = false;
			BtnPrev.Enabled = false;
		}
		else
		{
			BtnFirst.Enabled = true;
			BtnPrev.Enabled = true;
		}

		if (dv.PageIndex == dv.PageCount - 1)
		{
			BtnNext.Enabled = false;
			BtnLast.Enabled = false;
		}
		else
		{
			BtnNext.Enabled = true;
			BtnLast.Enabled = true;
		}
	}

	#endregion

	#region 按钮按下事件相应函数
	/// <summary>
	/// <para>功能描述：	查询所有用户</para>
	/// <para>输入参数：	</para>
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-02</para>
	/// <para>备注：	</para>
	/// </summary>
	protected void BtnSearch_OnClick(object sender, EventArgs e)
	{
		ViewState["UserGroupID"] = DdlSearchUserGroup.SelectedValue.Trim();
		BindGridView(GrvUsers, Convert.ToInt32(DdlSearchUserGroup.SelectedValue));
	}

	/// <summary>
	/// <para>功能描述：	根据选择的用户组查询用户</para>
	/// <para>输入参数：	</para>
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-02</para>
	/// <para>备注：	</para>
	/// </summary>
	protected void BtnSearchAll_OnClick(object sender, EventArgs e)
	{
		ViewState["UserGroupID"] = "0";
		BindGridView(GrvUsers, 0);
	}

	/// <summary>
	/// <para>功能描述：	全选反选按钮按下事件处理函数</para>
	/// <para>输入参数：	</para>
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-05</para>
	/// <para>备注：	</para>
	/// </summary>
	protected void BtnMultiSelect_OnClick(object sender, EventArgs e)
	{
		for (int i = 0; i < GrvUsers.Rows.Count; i++)
		{
			CheckBox Chk = (CheckBox)GrvUsers.Rows[i].FindControl("CbID");
			if (Chk.Checked == true)
			{
				Chk.Checked = false;
			}
			else
			{
				Chk.Checked = true;
			}
		}
	}

	/// <summary>
	/// <para>功能描述：	删除所选项按钮按下事件</para>
	/// <para>输入参数：	</para>
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-05</para>
	/// <para>备注：	</para>
	/// </summary>
	protected void BtnMultiDelete_OnClick(object sender, EventArgs e)
	{
		GridView dv;
		User clsUser;

		dv = GrvUsers;
		clsUser = new User();

		for (int i = 0; i < dv.Rows.Count; i++)
		{
			CheckBox Chk = (CheckBox)dv.Rows[i].FindControl("CbID");
			if (Chk.Checked == true)
			{
				clsUser.ID = Convert.ToInt32(dv.DataKeys[i].Value);
				clsUserComp.Delete(clsUser);
			}
		}

		BindGridView(dv, Convert.ToInt32(ViewState["UserGroupID"]));
	}

	/// <summary>
	/// <para>功能描述：	添加按钮按下相应函数</para>
	/// <para>输入参数：	</para>
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-05</para>
	/// <para>备注：	</para>
	/// </summary>
	protected void BtnAdd_OnClick(object sender, EventArgs e)
	{
		Views.SetActiveView(ViewAdd);
	}

	/// <summary>
	/// <para>功能描述：	用户列表按钮被按下时相应函数</para>
	/// <para>输入参数：	</para>
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-05</para>
	/// <para>备注：	</para>
	/// </summary>
	protected void BtnShow_OnClick(object sender, EventArgs e)
	{
		Views.SetActiveView(ViewShow);
	}

	/// <summary>
	/// <para>功能描述：	添加一个用户到数据库</para>
	/// <para>输入参数：	</para>
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-05</para>
	/// <para>备注：	</para>
	/// </summary>
	protected void BtnNew_OnClick(object sender, EventArgs e)
	{
		string strUsername;
		string strPassword;
		int nUserGroupID;
		int nDepartmentID;
		User clsUser;

		clsUser = new User();
		strUsername = TxtUsername.Text;
		strPassword = TxtPassword.Text;
		nUserGroupID = Convert.ToInt32(DdlUserGroupForAdd.Text);
		nDepartmentID = Convert.ToInt32(DdlDepartmentForAdd.Text);

		clsUser.Username = strUsername;
		clsUser.Password = strPassword;
		clsUser.UserGroupID = nUserGroupID;
		clsUser.DepartmentID = nDepartmentID;

		if (clsUserComp.Add(clsUser))
		{
			ShowMessageAlert.ShowMessage("添加成功！");
		}

		Views.SetActiveView(ViewShow);
		BindGridView(GrvUsers, Convert.ToInt32(ViewState["UserGroupID"]));
	}
	#endregion

	#region 分页控制按钮
	/// <summary>
	/// <para>功能描述：	下一页</para>
	/// <para>输入参数：	</para>
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-05</para>
	/// <para>备注：	</para>
	/// </summary>
	protected void BtnNext_OnClick(object sender, EventArgs e)
	{
		if (GrvUsers.PageIndex != GrvUsers.PageCount - 1)
		{
			GrvUsers.PageIndex = GrvUsers.PageIndex + 1;
		}
		BindGridView(GrvUsers);
	}

	/// <summary>
	/// <para>功能描述：	第一页</para>
	/// <para>输入参数：	</para>
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-05</para>
	/// <para>备注：	</para>
	/// </summary>
	protected void BtnFirst_OnClick(object sender, EventArgs e)
	{
		GrvUsers.PageIndex = 0;
		BindGridView(GrvUsers);
	}

	/// <summary>
	/// <para>功能描述：	上一页</para>
	/// <para>输入参数：	</para>
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-05</para>
	/// <para>备注：	</para>
	/// </summary>
	protected void BtnPrev_OnClick(object sender, EventArgs e)
	{
		if (GrvUsers.PageIndex != 0)
		{
			GrvUsers.PageIndex = GrvUsers.PageIndex - 1;
		}
		BindGridView(GrvUsers);
	}

	/// <summary>
	/// <para>功能描述：	最后一页</para>
	/// <para>输入参数：	</para>
	/// <para>返回值：	</para>
	/// <para>方法范围：	protected</para>
	/// <para>作者：	Tony Mucor</para>
	/// <para>日期：	2013-08-05</para>
	/// <para>备注：	</para>
	/// </summary>
	protected void BtnLast_OnClick(object sender, EventArgs e)
	{
		GrvUsers.PageIndex = GrvUsers.PageCount;
		BindGridView(GrvUsers);
	}
	#endregion

}
